package com.sduept.nwld.dataserver.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.PrimeFaces;
import org.primefaces.component.selectbooleancheckbox.SelectBooleanCheckbox;
import org.primefaces.component.selectoneradio.SelectOneRadio;
import org.primefaces.event.NodeCollapseEvent;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.NodeUnselectEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.ledger.enums.ManagementUnitEnum;
import com.sduept.cim.ledger.manager.SubstationLedgerManager;
import com.sduept.cim.ledger.model.ManagementUnitDict;
import com.sduept.cim.model.core.SubControlArea;
import com.sduept.cim.model.core.Substation;
import com.sduept.cim.query.service.SubstationQueryService;
import com.sduept.core.ParamManager;
import com.sduept.core.entity.SystemParameter;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import com.sduept.permission.PermissionParamDesc;
import com.sduept.permission.cache.PermissionCacheManager;
import com.sduept.permission.entity.DepartmentEntityBean;
import com.sduept.permission.entity.FunctionEntityBean;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.entity.RoleEntityBean;
import com.sduept.permission.enums.FunctionTypeEnum;
import com.sduept.permission.enums.PermissionTypeEnum;
import com.sduept.permission.exception.AuthorizationException;
import com.sduept.permission.manager.DepartManager;
import com.sduept.permission.manager.FunctionManager;
import com.sduept.permission.manager.PermissionManager;
import com.sduept.permission.manager.PersonnelManager;
import com.sduept.permission.manager.RoleManager;
import com.sduept.utils.MD5;

@Named
@ViewScoped
public class OrganizationPermissionController4Nw extends AbstractController {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7158662987193179534L;
	@Autowired
	private DepartManager dm;
	@Autowired
	private PersonnelManager pm;
	@Autowired
	private FunctionManager fm;
	@Autowired
	private PermissionManager perm;
	@Autowired
	private RoleManager rm;
	@Autowired
	private CimManager cimManager;
	@Autowired
	private SubstationQueryService subStationQueryService;
	@Autowired
	private PermissionCacheManager permissionCacheManager;
	@Autowired
	private SubstationLedgerManager substationLedgerManager;
	
	@Autowired
	private ParamManager paramMan;
	
	private TreeNode root;//组织机构树root
	private TreeNode roleRoot;//角色树
	private TreeNode rolePersonRoot;//人员树
	private TreeNode funcRoot;//当前节点授权树的root
	private TreeNode grantRoot;//弹开窗口授权树root
	private TreeNode selectedNode;
	private TreeNode[] selectedNodes;
	private TreeNode roleSelectedNode;
	private TreeNode[] roleSelectedNodes;
	
	private List<OrgFunctionDTO> dtos = new ArrayList<OrgFunctionDTO>();
	private List<OrgFunctionDTO> grantPermissionDtos = new ArrayList<OrgFunctionDTO>();
	private List<FunctionEntityBean> fbs;
	private List<RoleEntityBean> personRoles = new ArrayList<RoleEntityBean>();
	private RoleEntityBean currentSelectRole; // 当前选中的角色
	private String grantPerTitle;	//授权标题
	private Set<TreeNode> expandedSet = new HashSet<TreeNode>();//功能授权时展开过的节点
	private String stationSearch;//前台变电站查询
	private Map<String, List<OrgSubstationDTO>> orgSubstationMap = new TreeMap<String, List<OrgSubstationDTO>>();//管控区域-部门/人员的权限内部类
	private TreeNode substationRoot = new DefaultTreeNode();//变电站设置树
	private TreeNode orgSubstationRoot = new DefaultTreeNode();//变电站权限树
	Map<Integer, List<OrgSubstationDTO>> treeMap = new TreeMap<Integer, List<OrgSubstationDTO>>();//存放好电压等级-节点对象，变电站树节点展开时，直接获取展示节点。
	private Map<String, String> areaMap = new HashMap<String, String>();//存储管控区域（id-name）
	private Map<String, String> perMap = new HashMap<String, String>();//存储当前选择节点的权限值
	private String substationRadioValue;//变电站权限值  ALLOW DENY等等
	private Map<String, Map<String, String>> substationSavedMap = new HashMap<String, Map<String, String>>();//要存储的数据
	private HashMap<String, Substation> substationCacheMap = new HashMap<String, Substation>();
	private String updateMsg;//变电站设置时是否更新调管单位、运维单位提示
	private boolean value;
	private String initPwd = "Nfdwbhc@36621978";
	//以下是组织机构相关
	private TreeNode deptSelectedNode;//部门当前选择的节点
	private TreeNode deptRoot;//部门树
	private TreeNode parentNode;
	private DepartmentEntityBean currentDept;
	private PersonnelEntityBean currentPerson;
	private List<PersonnelEntityBean> personnels = new ArrayList<PersonnelEntityBean>();
	private Map<String, TreeNode> departmentMap = new HashMap<String, TreeNode>();//存储组织机构树的部门节点 部门id-部门节点
	private String oldPwd;//原密码
	private String newPwd;//新密码
	private String newPwd1;//新密码确认
	private boolean checkStatus;
	private String rolesName;
	private String code;
	private List<RoleEntityBean> roles = new ArrayList<RoleEntityBean>();
	private String[] selectedRoles;
	private String deptType;
	
	@PostConstruct
	public void init() {
		funcRoot = new DefaultTreeNode();
		grantRoot = new DefaultTreeNode();
		SystemParameter  p = paramMan.get(PermissionParamDesc.INIT_PASSWORD);
		initPwd = p==null ? initPwd : p.getValue();
		genStationid_map();
		fbs = fm.findRoots();
		initOrgTree();
		generateRoleRoot();
		initSubControlArea();//初始化管控区域
		genAllSubstationInfo();//生成变电站信息
		roles = rm.findAllRoles();//全部角色
	}
	 private void genStationid_map(){
		 substationCacheMap.clear();
	        List<Substation> stationList = subStationQueryService.listAll();
	        for(Substation station:stationList){
	        	substationCacheMap.put(station.getId(), station);
	        }
	    }
	/**
	 * 初始化组织机构树
	 */
	private void initOrgTree() {
		try {
			root = new LibTreeNode("Root", null);
			List<DepartmentEntityBean> departments = dm.findRootDepts();
			for (DepartmentEntityBean s : departments) {
				LibTreeNode n = new LibTreeNode("dept", s, root);
				departmentMap.put(s.getId(), n);//存储节点
				List<DepartmentEntityBean> ds = dm.getDepartmentsByParent(s.getId());
				if (ds != null && ds.size() > 0) {
					for (DepartmentEntityBean d : ds) {
						TreeNode treeNode = new LibTreeNode("dept", d, n);
						departmentMap.put(d.getId(), treeNode);//存储节点
					}
				}
				List<PersonnelEntityBean> personnels = dm.findDeptPersonnels(s.getId());
				if (personnels != null && personnels.size() > 0) {
					for (PersonnelEntityBean p : personnels) {
						new DefaultTreeNode("personnel", p, n);
					}
				}
				n.setExpanded(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void generateRoleRoot(){
		roleRoot = new LibTreeNode();
		LibTreeNode parent = new LibTreeNode("root","角色管理", roleRoot);
		List<RoleEntityBean> allRoles = rm.findAllRoles();
		for (RoleEntityBean role : allRoles) {
			new DefaultTreeNode(role, parent);
		}
		roleRoot.setExpanded(true);
		parent.setExpanded(true);
	}
	
	/**
	 * 准备授权
	 */
	public void preGrantPermission(){
		grantPermissionDtos.clear();
		grantRoot.getChildren().clear();
		if (null != roleSelectedNode) {
			generateRoleGrantPermissionTree();
		}else{
			generateGrantPermissionTree();
		}
		expandedSet.clear();//清空
	}
	
	/**
	 * 是否显示通知
	 * @return
	 */
	public boolean showNotice(){
		if(null == selectedNodes){
			return false;
		}
		return selectedNodes.length > 1;
	}
	/**
	 *  打开【功能授权】对话框后，初始化权限树，用于组织机构
	 */
	private void generateGrantPermissionTree() {
		Map<String,String> map=null;
		if(selectedNode.getData() instanceof DepartmentEntityBean){
			DepartmentEntityBean bean = (DepartmentEntityBean)selectedNode.getData();
			map = perm.findDeptResourcePermission(bean.getId());
			grantPerTitle = "【"+bean.getName()+"】"+"授权";
		}else{
			PersonnelEntityBean bean = (PersonnelEntityBean)selectedNode.getData();
			map = perm.findPersonResourcePermission(bean);
			grantPerTitle = "【"+bean.getName()+"】"+"授权";
		}
		for(FunctionEntityBean fb:fbs){
			OrgFunctionDTO rootDto = new OrgFunctionDTO(fb, "");
			grantPermissionDtos.add(rootDto);
			TreeNode subNode = new DefaultTreeNode(rootDto.getFunction().getType(), rootDto, grantRoot);
			subNode.setExpanded(true);
			generateGrantPermissionSubNode(subNode, fb.getSubFunctions(),map);
		}
	}
	
	/**
	 *   打开【功能授权】对话框后，初始化权限树用于角色节点
	 */
	private void generateRoleGrantPermissionTree() {
		if(currentSelectRole==null || StringUtils.isEmpty(currentSelectRole.getId())){
			return;
		}
		grantPerTitle = "【"+currentSelectRole.getName()+"】"+"授权";
		Map<String, String> map = perm.findRolePersonResourcePermission(currentSelectRole.getId());
		for(FunctionEntityBean fb:fbs){
			OrgFunctionDTO rootDto = new OrgFunctionDTO(fb, "");
			grantPermissionDtos.add(rootDto);
			TreeNode subNode = new DefaultTreeNode(rootDto.getFunction().getType(),rootDto, grantRoot);
			subNode.setExpanded(true);
			generateGrantPermissionSubNode(subNode, fb.getSubFunctions(), map);
		}
	}
	
	private void generateGrantPermissionSubNode(TreeNode parent,Collection<FunctionEntityBean> funs,Map<String,String> map) {
		for (FunctionEntityBean bean : funs) {
			OrgFunctionDTO dto = new OrgFunctionDTO(bean, "");
			TreeNode subNode = new DefaultTreeNode(dto.getFunction().getType(),dto, parent);
			grantPermissionDtos.add(dto);
			String permission = map.get(bean.getId());
			if(StringUtils.isNotEmpty(permission)){
				dto.setPermission(permission);
			}
			if (bean.getSubFunctions().size() > 0) {
				generateGrantPermissionSubNode(subNode, bean.getSubFunctions(),map);
			}
		}
	}

	public void preCreateRole() {
		List<TreeNode> subList = roleRoot.getChildren();
		for(TreeNode tn:subList){
			for(TreeNode stn:tn.getChildren()){
				stn.setSelected(false);
			}
		}
		currentSelectRole = new RoleEntityBean();
	}
	
	/**
	 * 保存角色
	 */
	public void saveRole() {
		rm.createOrUpdateRole(currentSelectRole);
		generateRoleRoot();
		roles = rm.findAllRoles();//全部角色
		currentSelectRole = null;
		addSuccessMessage("保存成功！");
	}
	
	public void deleteRole() {
		if (currentSelectRole == null) {
			return;
		}
		rm.deleteRole(currentSelectRole.getId());
		generateRoleRoot();
		roles = rm.findAllRoles();//全部角色
		dtos.clear();
		addSuccessMessage("删除成功!");
		currentSelectRole = null;
	}
	
	/**
	 * 角色、组织 授权
	 */
	public void save(){
		if (null != roleSelectedNode) {
			grantRolePermission();
		}else{
			grantOrgPermission();
		}
	}
	
	private void grantRolePermission(){
		Map<String, String> map = new HashMap<String, String>();
		for(OrgFunctionDTO dto:grantPermissionDtos){
			map.put(dto.getFunction().getId(), dto.getPermission());
		}
		if (null == roleSelectedNodes || roleSelectedNodes.length == 0) 
			return;
		TreeNode selNode = roleSelectedNodes[roleSelectedNodes.length - 1];
		RoleEntityBean bean = (RoleEntityBean) selNode.getData();
		String operator = bean.getId();
		perm.grantRolePermission(operator, map);
		loadSelectRolePermission();
		addSuccessMessage("授权成功!");
	}
	
	private void grantOrgPermission(){
		Map<String, String> map = new HashMap<String, String>();
		for(OrgFunctionDTO dto:grantPermissionDtos){
//			if (!StringUtils.isEmpty(dto.getPermission())) 
				map.put(dto.getFunction().getId(), dto.getPermission());
		}
		if (null == selectedNodes || selectedNodes.length == 0) 
			return;
		TreeNode node = selectedNodes[selectedNodes.length-1];
		Object obj = node.getData();
		if (obj instanceof PersonnelEntityBean) {
			PersonnelEntityBean p = (PersonnelEntityBean) obj;
			perm.grantPersonPermission(p.getCode(), map);
		}else if(obj instanceof DepartmentEntityBean){
			DepartmentEntityBean d = (DepartmentEntityBean) obj;
			perm.grantDeptPermission(d.getId(), map);
		}
		loadSelectOrgPermission();
		addSuccessMessage("授权成功！");
	}
	
	public void changeRadio(AjaxBehaviorEvent event){ 
		SelectOneRadio radio = (SelectOneRadio)event.getComponent();
		setupCurrentNodePermission(grantRoot,radio.getWidgetVar());
		expandNodeByExpanded();//自动展开功能授权时展开过的节点，解决primefaces自动展开问题。
	}
	
	private void setupCurrentNodePermission(TreeNode root,String funId){
		for(TreeNode subtn:root.getChildren()){
			OrgFunctionDTO dto = (OrgFunctionDTO)subtn.getData();
			if(funId.equals(dto.getFunction().getId())){
				OrgFunctionDTO pdto = (OrgFunctionDTO)subtn.getParent().getData();
				if(FunctionTypeEnum.approot.toString().equals(pdto.getFunction().getType()) || FunctionTypeEnum.root.toString().equals(pdto.getFunction().getType())){
					setSubTreeNodePermission(dto.getPermission(),subtn.getChildren());
				}else{
					if(PermissionTypeEnum.ALLOW.toString().equals(pdto.getPermission())){
						setSubTreeNodePermission(dto.getPermission(),subtn.getChildren());
					}//权限优化为统一处理
				}
				changeParentPermissionByNode(subtn);//根据当前传入的节点，递归，根据其同级节点的权限值，修改父节点的值
				changeChildPermissionByNode(subtn);//根据当前传入的节点，递归，根据父节点的权限值，修改子点的值
			}else{//优化：当前节点是要找的节点则不去遍历子节点
				if(subtn.getChildCount()>0){
					setupCurrentNodePermission(subtn,funId);
				}
			}
		}
	}
	private void setSubTreeNodePermission(String parentPermisison,List<TreeNode> sublist){
		for(TreeNode tn:sublist){
			OrgFunctionDTO dto = (OrgFunctionDTO)tn.getData();
			if(FunctionTypeEnum.menu.toString().equals(dto.getFunction().getType()) || 
					FunctionTypeEnum.pagemenu.toString().equals(dto.getFunction().getType()) || 
					FunctionTypeEnum.appmenu.toString().equals(dto.getFunction().getType())){
				dto.setPermission(parentPermisison);
				setSubTreeNodePermission(dto.getPermission(),tn.getChildren());
			}//权限优化为统一处理
		}
	}
	
	/**
	 * 根据当前传入的节点，递归，根据其同级节点的权限值，修改父节点的值
	 * @param treeNode 节点
	 */
	private void changeParentPermissionByNode(TreeNode treeNode){
		Set<String> permissionSet = new HashSet<String>();
		OrgFunctionDTO orgDto = (OrgFunctionDTO) treeNode.getData();
		if(!FunctionTypeEnum.operation.toString().equals(orgDto.getFunction().getType())){//不是操作
			TreeNode parentNode = treeNode.getParent();
			OrgFunctionDTO ofd = (OrgFunctionDTO) parentNode.getData();
			if(!FunctionTypeEnum.approot.toString().equals(ofd.getFunction().getType()) && 
					!FunctionTypeEnum.root.toString().equals(ofd.getFunction().getType())){//不是根节点
				if(null != parentNode && parentNode.getChildren().size() > 0){
					for(TreeNode node : parentNode.getChildren()){
						OrgFunctionDTO dto = (OrgFunctionDTO) node.getData();
						permissionSet.add(dto.getPermission());
					}
				}
				if(permissionSet.size() == 1){//权限值一致
					Iterator<String> permission = permissionSet.iterator();
					ofd.setPermission(permission.next());
				}else if(permissionSet.size() > 1){
					boolean hasAllow = false;//是否有Allow
					for(String str : permissionSet){
						if(StringUtils.isNotEmpty(str) && str.equals(PermissionTypeEnum.ALLOW.toString())){
							hasAllow = true;
							break;
						}
					}
					if(hasAllow){
						ofd.setPermission(PermissionTypeEnum.ALLOW.toString());
					}else{
						ofd.setPermission(PermissionTypeEnum.DENY.toString());
					}
				}
				if(null != parentNode.getParent() && parentNode.getChildren().size() > 0){//递归
					changeParentPermissionByNode(parentNode);
				}
			}
		}
	}
	
	/**
	 * 根据当前传入的节点，递归，根据父节点的权限值，修改子点的值
	 * @param treeNode 节点
	 */
	private void changeChildPermissionByNode(TreeNode treeNode){
		OrgFunctionDTO orgDto = (OrgFunctionDTO) treeNode.getData();
		if(!FunctionTypeEnum.operation.toString().equals(orgDto.getFunction().getType())){//不是操作
			if(treeNode.getChildCount() > 0){//有值
				for(TreeNode node : treeNode.getChildren()){
					OrgFunctionDTO ofd = (OrgFunctionDTO) node.getData();
					if(!FunctionTypeEnum.operation.toString().equals(ofd.getFunction().getType())){//不是操作
						ofd.setPermission(orgDto.getPermission());
					}
					if(node.getChildCount() > 0){
						changeChildPermissionByNode(node);
					}
				}
			}
		}
	}
	
	/**
	 * 自动展开功能授权时展开过的节点，解决primefaces自动展开问题。
	 */
	public void expandNodeByExpanded(){
		for(TreeNode treeNode : expandedSet){
			treeNode.setExpanded(true);
		}
	}
	
	/**
	 * 组织机构树节点展开事件
	 * @param event
	 */
	public void onNodeExpand(NodeExpandEvent event) {
		TreeNode selected = event.getTreeNode();
		selected.getChildren().clear();
		if (selected.getType().equals("dept")) {
			DepartmentEntityBean dept = (DepartmentEntityBean) selected.getData();
			try {
				List<DepartmentEntityBean> ds = dm.getDepartmentsByParent(dept.getId());
				if (ds != null && ds.size() > 0) {
					for (DepartmentEntityBean d : ds) {
						TreeNode treeNode = new LibTreeNode("dept", d, selected);
						departmentMap.put(d.getId(), treeNode);//存储节点
					}
				} 
				personnels = dm.findDeptPersonnels(dept.getId());
				if (personnels != null && personnels.size() > 0) {
					for (PersonnelEntityBean p : personnels) {
						new DefaultTreeNode("personnel", p, selected);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 组织机构树节点选择事件
	 */
	public void onNodeSelect(NodeSelectEvent event) {
		selectedNode = event.getTreeNode();
		loadSelectOrgPermission();
		loadPersonRoles();
//		//两个树选择节点进行互斥
		unExpandTreeNode(roleRoot);
		unSelectedNode(roleRoot);
		roleSelectedNode = null;
		currentSelectRole = null;
		roleSelectedNodes = null;
		loadOrgSubstationTree();//生成变电站权限树
		loadPersonnelList(selectedNode);//加载员工信息列表
	}
	
	/**
	 * 角色树节点选择事件
	 * @param event
	 */
	public void onRoleNodeSelect(NodeSelectEvent event){
		roleSelectedNode = event.getTreeNode();
		if (roleSelectedNode.getData() instanceof RoleEntityBean) {
			currentSelectRole = (RoleEntityBean) roleSelectedNode.getData();
			loadSelectRolePermission();
			generateRolePersonTree();
		}else{
			currentSelectRole = null;
		}
		//两个树选择节点进行互斥
		unExpandTreeNode(root);
		unSelectedNode(root);
		selectedNode = null;
		selectedNodes = null;
		currentDept = null;
		currentPerson = null;
	}
	
	private void loadSelectOrgPermission(){
		Map<String, String> map = new HashMap<String, String>();
		if (selectedNode.getType().equals("dept")) {
			DepartmentEntityBean selDepart = (DepartmentEntityBean) selectedNode.getData();
			map = perm.findDeptResourcePermission(selDepart.getId());
		} else {
			PersonnelEntityBean selPerson = (PersonnelEntityBean) selectedNode.getData();
			map = perm.findPersonResourcePermission(selPerson);
		}
		generatedSelectFunctionTree(map);
	}

	private void loadSelectRolePermission(){
		RoleEntityBean role = (RoleEntityBean) roleSelectedNode.getData();
		Map<String, String> map = perm.findRolePersonResourcePermission(role.getId());
		generatedSelectFunctionTree(map);
	}
	
	public void onNodeUnSelect(NodeUnselectEvent event){
		selectedNode = null;
		if (selectedNodes.length > 0) {
			selectedNode = selectedNodes[0];
		}
		currentDept = null;
		currentPerson = null;
		personnels.clear();
	}
	
	public void onRoleNodeUnSelect(NodeUnselectEvent event){
		roleSelectedNode = null;
		currentSelectRole = null;
		if (roleSelectedNodes.length > 0) {
			roleSelectedNode = roleSelectedNodes[0];
			currentSelectRole = (RoleEntityBean) roleSelectedNode.getData();
		}
	}
	
	/**
	 * 功能授权时功能树节点展开事件
	 * @param event
	 */
	public void onFunNodeExpand(NodeExpandEvent event) {
		TreeNode treeNode = event.getTreeNode();
		expandedSet.add(treeNode);
	}
	
	/**
	 * 功能授权时功能树节点关闭事件
	 * @param event
	 */
	public void onFunNodeCollapse(NodeCollapseEvent event) {
		TreeNode treeNode = event.getTreeNode();
		expandedSet.remove(treeNode);
	}
	
	/**
	 * 生成当前选择的节点具备的权限 包括允许 和拒绝的
	 */
	private void generatedSelectFunctionTree(Map<String, String> map) {
		funcRoot.getChildren().clear();
		for (FunctionEntityBean fb:fbs) {
			OrgFunctionDTO rootDto = new OrgFunctionDTO(fb, "");
			dtos.add(rootDto);
			TreeNode subNode = new DefaultTreeNode(rootDto.getFunction().getType(), rootDto, funcRoot);
			subNode.setExpanded(true);
			generateSelectFunctionSubNode(subNode, fb.getSubFunctions(),map);
			if (subNode.getChildCount() == 0) {
				funcRoot.getChildren().remove(subNode);
				dtos.remove(rootDto);
			}
		}
	}

	/**
	 * 递归生成子节点
	 */
	private void generateSelectFunctionSubNode(TreeNode parent,List<FunctionEntityBean> funs,Map<String, String> map) {
		Collections.sort(funs,new Comparator<FunctionEntityBean>() {
			@Override
			public int compare(FunctionEntityBean o1, FunctionEntityBean o2) {
				return o1.getOrderNo()-o2.getOrderNo();
			}
		});
		for (FunctionEntityBean bean : funs) {
			if(map.get(bean.getId())!=null){
				OrgFunctionDTO dto = new OrgFunctionDTO(bean, map.get(bean.getId()));
				dtos.add(dto);
				TreeNode subNode = new DefaultTreeNode(dto.getFunction().getType(),dto, parent);
				if (dto.getFunction().getSubFunctions().size() > 0) {
					generateSelectFunctionSubNode(subNode, bean.getSubFunctions(),map);
				}
			}
		}
	}
	
	private void unSelectedNode(TreeNode node){
		node.setSelected(false);
		for (TreeNode n : node.getChildren()) {
			unSelectedNode(n);
		}
	}
	
	private void unExpandTreeNode(TreeNode node){
		node.setExpanded(false);
		for (TreeNode n : node.getChildren()) {
			n.setExpanded(false);
		}
	}
	
	private void loadPersonRoles(){
		Object obj = selectedNode.getData();
		if (obj instanceof PersonnelEntityBean) {
			personRoles.clear();
			PersonnelEntityBean p = (PersonnelEntityBean) obj;
			personRoles.addAll(pm.findPersonRolesByCode(p.getCode()));
		}
	}
	
	private void generateRolePersonTree(){
		List<PersonnelEntityBean> rolePersonnels = new ArrayList<PersonnelEntityBean>();
		try {
			rolePersonnels = rm.findRolePersonnels(currentSelectRole.getId());
		}catch (Exception e) {e.printStackTrace();}
		
		rolePersonRoot = new DefaultTreeNode();
		List<RolePersonDTO> dtos = new ArrayList<OrganizationPermissionController4Nw.RolePersonDTO>();
		for (PersonnelEntityBean p : rolePersonnels) {
			List<DepartmentEntityBean>departs = new ArrayList<DepartmentEntityBean>();
			DepartmentEntityBean depart = p.getGroup();
			departs = dm.recursiveFindParentDepartments(depart);
			departs.add(depart);
			TreeNode parent = rolePersonRoot;
			outher: for (DepartmentEntityBean d : departs) {
				RolePersonDTO dto = new RolePersonDTO(d.getName(),d.getTelephone(),"--");
				if (dtos.contains(dto)) {
					for (TreeNode node : parent.getChildren()) {
						if (node.getData().equals(dto)) {
							parent = node;
							continue outher;
						}
					}
				}
				DefaultTreeNode node = new DefaultTreeNode(dto, parent);
				if (null == parent.getParent()) 
					node.setExpanded(true);
				parent = node;
				dtos.add(dto);
			}
			new DefaultTreeNode(new RolePersonDTO(p.getName(), p.getTelephone(), p.getLocked()?"锁定":"正常"), parent);
		}
	}
	
	public void openGrantRoleDialog(){
		Map<String,Object> options = new HashMap<String, Object>();
	    options.put("contentWidth", "1400");
	    options.put("contentHeight", "600");
	    options.put("resizable", true);
 		options.put("draggable", true);
 		options.put("modal", true);
		Map<String, List<String>> params = new HashMap<String, List<String>>();
 		List<String> p = new ArrayList<String>();
 		List<PersonnelEntityBean> persons = filterPersons();
 		for (PersonnelEntityBean pb : persons) 
 			p.add(pb.getCode());
 		if(null == p || p.size() == 0){
 			addErrorMessage("该部门为空，无法进行角色设置！");
 		}else{
 			params.put("code", p);
 			openDialog("/ui/org/permission/grantRoleManager",params ,options);
 		}
	}
	
	private List<PersonnelEntityBean> filterPersons(){
		List<PersonnelEntityBean> persons = new ArrayList<PersonnelEntityBean>();
 		List<DepartmentEntityBean> departs = new ArrayList<DepartmentEntityBean>();
 		for (TreeNode node : selectedNodes) {
			Object object = node.getData();
			if (object instanceof PersonnelEntityBean) {
				persons.add((PersonnelEntityBean) object);
			}else if(object instanceof DepartmentEntityBean){
				departs.add((DepartmentEntityBean) object);
			}
		}
 		Set<DepartmentEntityBean> needDeleteList = new HashSet<>();
 		outer:
 		//去除指定人员的所有父级部门
 		for (PersonnelEntityBean per : persons) {
 			for(DepartmentEntityBean d:dm.recursiveFindParentDepartments(per)){
 				if(departs.contains(d)){
 					needDeleteList.add(d);
 					continue outer;
 				}
 			}
 		}
 		//去除指定部门的所有上级部门
		outer:
 		for (DepartmentEntityBean d : departs) {
 			for (DepartmentEntityBean pd :  dm.recursiveFindParentDepartments(d)) {
 				if(departs.contains(pd)){
 					needDeleteList.add(pd);
 					continue outer;
	 			}
			}
		}
		departs.removeAll(needDeleteList);
 		//查找其他部门的所有人员
 		for (DepartmentEntityBean d : departs) {
			persons.addAll(dm.recursiveFindDepartPersonnels(d.getId()));
		}
 		return persons;
	}
	
	public void dialogCallBack(SelectEvent event){
		int count = (int) event.getObject();
		for (TreeNode node : selectedNodes) {
			if (node.getData() instanceof PersonnelEntityBean) {
//				if (count > 0) {//注：此处原先用来区分有角色的和无角色的人员，后来弃用了。
//					node.setType("personnelr");
//				}else{
					node.setType("personnel");
//				}
			}else{
				for (TreeNode n : node.getChildren()) {
					if (n.getType().equals("dept"))
						continue;
					
					if (count > 0) {
						n.setType("personnelr");
					}else{
						n.setType("personnel");
					}
				}
			}
		}
		loadSelectOrgPermission();
		loadPersonRoles();
		addSuccessMessage("角色设置成功!");
	}
	
	/**
	 * 生成变电站信息，组织电压等级-部门/人员的权限内部类
	 */
	public void genAllSubstationInfo(){
		orgSubstationMap.clear();
		HashMap<String, Substation> substationMap = substationCacheMap;
		for(Map.Entry<String, Substation> station : substationMap.entrySet()){
			Substation sub = station.getValue();
			OrgSubstationDTO org = genOrgSubstationDTO(sub.getId(), sub.getId(), sub.getName(), sub.getId(), false, 
				String.valueOf(sub.getStandVoltagelevel()));//dto
			if(null == sub.getSubControlArea() || StringUtils.isEmpty(areaMap.get(sub.getSubControlArea()))){
				List<OrgSubstationDTO> orgList = orgSubstationMap.get("无管控区域");
				if(null == orgList){
					orgList = new ArrayList<OrgSubstationDTO>();
					orgList.add(org);
					orgSubstationMap.put("无管控区域", orgList);
				}else{
					orgList.add(org);
				}
				continue;
			}
			String areaName = areaMap.get(sub.getSubControlArea());//管控区域名称
			List<OrgSubstationDTO> orgList = orgSubstationMap.get(areaName);
			if(null == orgList){
				orgList = new ArrayList<OrgSubstationDTO>();
				orgList.add(org);
				orgSubstationMap.put(areaName, orgList);
			}else{
				orgList.add(org);
			}
		}
	}
	
	/**
	 * 初始化管控区域
	 */
	public void initSubControlArea(){
		areaMap.clear();
		List<SubControlArea> subControlAreas = cimManager.findAllSubControlAreas();
		for(SubControlArea area : subControlAreas){
//			areaMap.put(area.getId(), area.getName());
			areaMap.put(area.getRdfId(), area.getName());
		}
	}
	
	/**
	 * 生成全部变电站树，并根据当前所选节点的权限值修改变电站树的权限值
	 */
	public void genAllSubstationTree(){
		genAllSubstationInfo();
		substationRoot = new DefaultTreeNode();
		for(Entry<String, List<OrgSubstationDTO>> entry : orgSubstationMap.entrySet()){
			String id = ObjectId.get().toString();
			OrgSubstationDTO org = genOrgSubstationDTO(id, id, entry.getKey(), id, false, entry.getKey());//dto
			TreeNode subNode = new DefaultTreeNode(entry.getKey(), org, substationRoot);
			List<OrgSubstationDTO> subList = entry.getValue();
			for(OrgSubstationDTO orgs : subList){
				new DefaultTreeNode(orgs.getId(), orgs, subNode);
			}
		}
		changePermissionBySelected();//根据当前选择节点的权限值修改变电站树的权限值
	}
	
	/**
	 * 根据权限数据生成变电站树
	 */
	public void genSubstationTreeByPermission(){
		for(Map.Entry<String, List<OrgSubstationDTO>> entry : orgSubstationMap.entrySet()){
			String id = ObjectId.get().toString();
			OrgSubstationDTO org = genOrgSubstationDTO(id, id, entry.getKey(), id, false, entry.getKey());//dto
			TreeNode subNode = new DefaultTreeNode(entry.getKey(), org, substationRoot);
			List<OrgSubstationDTO> subList = entry.getValue();
			for(OrgSubstationDTO orgs : subList){
				new DefaultTreeNode(orgs.getId(), orgs, subNode);
			}
		}
//		changePermissionBySelected();//根据当前选择节点的权限值修改变电站树的权限值
	}
	
	/**
	 * 生成变电站权限树
	 */
	public void loadOrgSubstationTree(){
		if(null != selectedNode){
			orgSubstationRoot = new DefaultTreeNode();
			if(selectedNode.getData() instanceof DepartmentEntityBean){//部门
				DepartmentEntityBean depart = (DepartmentEntityBean) selectedNode.getData();
				genOrgSubstationTreeByDepart(depart);
			}
		}
	}
	
	/**
	 * 根据所选部门生成变电站权限树
	 * @param depart 所选部门
	 */
	public void genOrgSubstationTreeByDepart(DepartmentEntityBean depart){
		perMap.clear();//清空
		perMap = perm.findDeptResourcePermission(depart.getId());
		orgSubstationRoot = new DefaultTreeNode();
//		Map<Integer, List<OrgSubstationDTO>> treeMap = new TreeMap<Integer, List<OrgSubstationDTO>>();//存放好电压等级-节点对象
		treeMap.clear();//清空
		for(Map.Entry<String, String> entry : perMap.entrySet()){
			String permission = entry.getValue();//权限
			if(PermissionTypeEnum.ALLOW.toString().equals(permission)){//只显示允许的数据
				Substation sub = substationCacheMap.get(entry.getKey());//查询变电站信息
				if(sub==null)//无对应信息
					continue;
//				String nodeName = (int) sub.getStandVoltagelevel() + "kV";
				int nodeName = (int) sub.getStandVoltagelevel();
				List<OrgSubstationDTO> orgList = treeMap.get(nodeName);
				OrgSubstationDTO org = genOrgSubstationDTO(sub.getId(), depart.getId(), sub.getName(), entry.getKey(), 
					PermissionTypeEnum.ALLOW.toString().equals(permission)?true:false, String.valueOf(sub.getStandVoltagelevel()));//dto
				if(null == orgList){
					orgList = new ArrayList<OrgSubstationDTO>();
					orgList.add(org);
					treeMap.put(nodeName, orgList);
				}else{
					orgList.add(org);
				}
			}
		}
		for(Map.Entry<Integer, List<OrgSubstationDTO>> entry : treeMap.entrySet()){
			if(null != entry.getValue() && entry.getValue().size() > 0){//过滤掉无效外层节点
				String id = ObjectId.get().toString();
				OrgSubstationDTO org = genOrgSubstationDTO(id, id, String.valueOf(entry.getKey())+ "kV", id, false, null);//dto
				if(entry.getKey() != 0){//不显示0kV的
					new LibTreeNode(id, org, orgSubstationRoot);//外层节点
					//原直接初始化子节点优化为变电站树节点展开时再初始化。
				}
			}
		}
	}
	
	/**
	 * 前台变电站树节点展开事件，节点展开时再初始化子节点。
	 * @param event
	 */
	public void onStationNodeExpand(NodeExpandEvent event) {
		TreeNode selected = event.getTreeNode();
		selected.getChildren().clear();//清空
		OrgSubstationDTO org = (OrgSubstationDTO) selected.getData();
		int key = Integer.parseInt(org.getName().replace("kV", ""));
		List<OrgSubstationDTO> orgList = treeMap.get(key);
		for(OrgSubstationDTO orgs : orgList){
			if(true == orgs.permission){//只显示允许的数据
				new DefaultTreeNode(orgs.getId(), orgs, selected);
			}
		}
	}
	
	/**
	 * 变电站设置前校验
	 */
	public void preSettingSubstation(){
		stationSearch = null;//重置查询
		substationSavedMap.clear();//清空
		genOrgSubstationTree();//根据选择的部门生成变电站树
	}
	
	/**
	 * 根据选择的部门生成变电站树
	 */
	public void genOrgSubstationTree(){
		substationRoot = new DefaultTreeNode();
		if(null != selectedNode){
			if(selectedNode.getData() instanceof DepartmentEntityBean){//部门 人员权限已去除
				DepartmentEntityBean depart = (DepartmentEntityBean) selectedNode.getData();
				perMap.clear();//清空
//				perMap = perm.findDeptResourcePermission(depart.getId());
				perMap = perm.findOperatorResources(depart.getId());
				if(null == depart.getParent()){//无父，为顶级部门。展示全部变电站
					genAllSubstationTree();//生成全部变电站树，并根据当前所选节点的权限值修改变电站树的权限值
				}else{//有父，根据父级权限来构建
					genSubstationTreeBySelected();
				}
			}
		}
	}
	
	/**
	 * 根据选择的部门生成变电站树，并根据当前所选节点的权限值修改变电站树的权限值
	 */
	public void genSubstationTreeBySelected(){
		if(null != selectedNode){
			if(selectedNode.getData() instanceof DepartmentEntityBean){//部门（人员权限已去除）
				DepartmentEntityBean depart = (DepartmentEntityBean) selectedNode.getData();
				Map<String, String> permissionMap = getParentSubstation(depart);//递归，获取当前所选节点的最近父节点的权限。
				substationRoot = new DefaultTreeNode();
				if(null != permissionMap && permissionMap.size() > 0){
					orgSubstationMap.clear();//清空
					for(Map.Entry<String, String> entry : permissionMap.entrySet()){
						if(PermissionTypeEnum.ALLOW.toString().equals(entry.getValue())){//只展示允许的
							Substation sub = substationCacheMap.get(entry.getKey());//变电站
							if(null == sub)
								continue;//去除功能权限
							String areaName = areaMap.get(sub.getSubControlArea());//区域名称
							OrgSubstationDTO org = genOrgSubstationDTO(sub.getId(), sub.getId(), sub.getName(), sub.getId(), false, 
								String.valueOf(sub.getStandVoltagelevel()));//dto
							if(StringUtils.isEmpty(areaName)){//无调管区域
								List<OrgSubstationDTO> orgList = orgSubstationMap.get("无管控区域");
								if(null == orgList){
									orgList = new ArrayList<OrgSubstationDTO>();
									orgList.add(org);
									orgSubstationMap.put("无管控区域", orgList);
								}else{
									orgList.add(org);
								}
								continue;
							}else{
								List<OrgSubstationDTO> orgList = orgSubstationMap.get(areaName);
								if(null == orgList){
									orgList = new ArrayList<OrgSubstationDTO>();
									orgList.add(org);
									orgSubstationMap.put(areaName, orgList);
								}else{
									orgList.add(org);
								}
							}
						}
					}
					genSubstationTreeByPermission();//根据权限数据生成变电站树
					changePermissionBySelected();//根据当前选择节点的权限值修改变电站树的权限值
				}
			}
		}
	}
	
	/**
	 * 递归，获取当前所选节点的最近父节点的权限。
	 * @param depart
	 * @return
	 */
	public Map<String, String> getParentSubstation(DepartmentEntityBean depart){
		Map<String, String> persMap = null;
		DepartmentEntityBean parent = depart.getParent();//父部门
		if(null != parent){
			persMap = perm.findOperatorResources(parent.getId());
			//注：为防止子部门的变电站权限不比父部门多，所以不进行递归，只查询当前节点一级父部门的权限。
//			perMap = perm.findDeptResourcePermission(parent.getId());
//			if(null == perMap || perMap.size() == 0){
//				getParentSubstation(parent);
//			}
		}
		return persMap;
	}
	
	/**
	 * 根据当前选择节点的权限值修改变电站树的权限值
	 */
	public void changePermissionBySelected(){
		if(null != selectedNode && null != perMap && perMap.size() > 0){
			if(selectedNode.getData() instanceof DepartmentEntityBean){//部门
				DepartmentEntityBean depart = (DepartmentEntityBean) selectedNode.getData();
				String operator = depart.getId();//操作者
				for(TreeNode node : substationRoot.getChildren()){
					OrgSubstationDTO orgs = (OrgSubstationDTO) node.getData();//dto
					orgs.setOperator(operator);
					Set<Boolean> permissionSet = new HashSet<Boolean>();//存储permission
					for(TreeNode tn : node.getChildren()){
						OrgSubstationDTO org = (OrgSubstationDTO) tn.getData();//dto
						org.setOperator(operator);//操作者
						String permission = perMap.get(org.getId());//拿到当前变电站对应的所部门的权限值
						if(StringUtils.isNotEmpty(permission)){
							if(PermissionTypeEnum.ALLOW.toString().equals(permission)){
								org.setPermission(true);
							}else{
								org.setPermission(false);
							}
						}
						permissionSet.add(org.getPermission());
					}
					if(permissionSet.size() == 1){//权限值一致
						Iterator<Boolean> permission = permissionSet.iterator();
						orgs.setPermission(permission.next());
					}else if(permissionSet.size() > 1){
						orgs.setPermission(true);
					}
				}
			}
		}
	}
	
	/**
	 * 根据变电站名称检索
	 */
	public void searchBySubstation(){
		if(StringUtils.isNotEmpty(stationSearch) && !stationSearch.equals("全部")){
			for(TreeNode tn : substationRoot.getChildren()){
				tn.setExpanded(false);
				for(TreeNode trn : tn.getChildren()){
					OrgSubstationDTO org = (OrgSubstationDTO) trn.getData();//dto
					trn.setSelected(false);
					if(stationSearch.equals(org.getName())){
						tn.setExpanded(true);
						trn.setSelected(true);
					}
				}
			}
		}else{
			for(TreeNode tn : substationRoot.getChildren()){
				tn.setExpanded(false);
				for(TreeNode trn : tn.getChildren()){
					trn.setSelected(false);
				}
			}
		}
	}
	
	/**
	 * 重置
	 */
	public void resetSubstation(){
		stationSearch = null;
		searchBySubstation();
	}
	
	/**
	 * 变电站设置更换权限
	 * @param event
	 */
	public void substationRadio(AjaxBehaviorEvent event){
		SelectBooleanCheckbox checkbox = (SelectBooleanCheckbox) event.getComponent();
		if(selectedNode.getData() instanceof DepartmentEntityBean){//部门
			DepartmentEntityBean depart = (DepartmentEntityBean) selectedNode.getData();
			boolean permission = (boolean) checkbox.getValue();
			OrgSubstationDTO osd = genOrgSubstationDTO(checkbox.getWidgetVar(), depart.getId(), null, checkbox.getWidgetVar(), 
				permission, null);
			changeAllSubstationTree(osd);//根据当前节点修改对应的变电站树的状态
		}else{//人员
			PersonnelEntityBean person = (PersonnelEntityBean) selectedNode.getData();
			boolean permission = (boolean) checkbox.getValue();
			OrgSubstationDTO osd = genOrgSubstationDTO(checkbox.getWidgetVar(), person.getId(), null, checkbox.getWidgetVar(), 
				permission, null);
			changeAllSubstationTree(osd);//根据当前节点修改对应的变电站树的状态
		}
	}
	
	/**
	 * 根据当前节点修改对应的变电站树的状态
	 * @param osd
	 */
	public void changeAllSubstationTree(OrgSubstationDTO osd){
		Map<String, String> savedMap = substationSavedMap.get(osd.getOperator());
		boolean permission = osd.getPermission();
		boolean find = false;//是否找到节点
		for(TreeNode treeNode : substationRoot.getChildren()){//外层节点
			treeNode.setExpanded(false);//关闭节点，解决primeface自动展开展开过的节点bug
			OrgSubstationDTO orgs = (OrgSubstationDTO) treeNode.getData();
			if(osd.getId().equals(orgs.getId())){
				treeNode.setExpanded(true);
				orgs.setPermission(permission);
				Set<Boolean> permissionSet = new HashSet<Boolean>();//存储permission
				for(TreeNode tn : treeNode.getChildren()){//子节点和父节点权限保持一致
					OrgSubstationDTO org = (OrgSubstationDTO) tn.getData();
					org.setPermission(permission);
					if(null != savedMap){
						savedMap.put(org.getId(), true == osd.getPermission() ? PermissionTypeEnum.ALLOW.toString() : PermissionTypeEnum.DENY.toString());
						substationSavedMap.put(osd.getOperator(), savedMap);
					}else{
						savedMap = new HashMap<String, String>();
						savedMap.put(org.getId(), true == osd.getPermission() ? PermissionTypeEnum.ALLOW.toString() : PermissionTypeEnum.DENY.toString());
						substationSavedMap.put(osd.getOperator(), savedMap);
					}
//					savedMap.put(org.getId(), osd.getPermission());
//					substationSavedMap.put(osd.getOperator(), savedMap);
					permissionSet.add(org.getPermission());
				}
//				return;//为解决primeface自动展开展开过的节点bug，找到对应节点后不直接结束，而是继续关闭其他节点。
				find = true;
			}
			if(!find){//外层节点不对应则找内层
				Set<Boolean> permissionSet = new HashSet<Boolean>();//存储permission
				for(TreeNode node : treeNode.getChildren()){//内层节点
					OrgSubstationDTO org = (OrgSubstationDTO) node.getData();
					permissionSet.add(org.getPermission());
					if(osd.getId().equals(org.getId())){
						org.setPermission(permission);
						node.getParent().setExpanded(true);//打开父节点
						if(null != savedMap){
							savedMap.put(org.getId(), true == osd.getPermission() ? PermissionTypeEnum.ALLOW.toString() : PermissionTypeEnum.DENY.toString());
							substationSavedMap.put(osd.getOperator(), savedMap);
						}else{
							savedMap = new HashMap<String, String>();
							savedMap.put(org.getId(), true == osd.getPermission() ? PermissionTypeEnum.ALLOW.toString() : PermissionTypeEnum.DENY.toString());
							substationSavedMap.put(osd.getOperator(), savedMap);
						}
//						return;//为动态修改父节点的状态不直接结束
					}
				}
				if(permissionSet.size() == 1){//权限值一致
					Iterator<Boolean> ite = permissionSet.iterator();
					orgs.setPermission(ite.next());
				}else if(permissionSet.size() > 1){
					orgs.setPermission(true);
				}
			}
		}
	}
	
	/**
	 * 全选
	 */
	public void substationCheckAll(){
		changeAllTreeByPermission(true);
	}
	
	/**
	 * 反选
	 */
	public void substationUnCheckAll(){
		changeAllTreeByPermission(false);
	}
	
	/**
	 * 根据权限修改
	 * @param permission
	 */
	public void changeAllTreeByPermission(boolean permission){
		if(selectedNode.getData() instanceof DepartmentEntityBean){//部门
			DepartmentEntityBean depart = (DepartmentEntityBean) selectedNode.getData();
			Map<String, String> permissionMap = new HashMap<String, String>();
			for(TreeNode treeNode : substationRoot.getChildren()){
				OrgSubstationDTO orgs = (OrgSubstationDTO) treeNode.getData();//dto
				orgs.setPermission(permission);
				for(TreeNode tn : treeNode.getChildren()){
					OrgSubstationDTO org = (OrgSubstationDTO) tn.getData();//dto
					org.setPermission(permission);
					permissionMap.put(org.getId(),  true == permission ? PermissionTypeEnum.ALLOW.toString() : PermissionTypeEnum.DENY.toString());
				}
			}
			substationSavedMap.clear();
			substationSavedMap.put(depart.getId(), permissionMap);
		}
	}
	
	/**
	 * 变电站设置
	 */
	public void substationSetting(){
		saveAllOrgSubstation();//保存变电站设置的权限
		addSuccessMessage("变电站设置成功！");
	}
	
	/**
	 * 保存变电站设置的权限
	 */
	public void saveAllOrgSubstation(){
		if(null != selectedNode){
			if(selectedNode.getData() instanceof DepartmentEntityBean){//部门
				DepartmentEntityBean depart = (DepartmentEntityBean) selectedNode.getData();
				if(null != substationSavedMap.get(depart.getId())){
					Map<String, String> permissionMap = substationSavedMap.get(depart.getId());
					DepartmentEntityBean parentDepart = depart.getParent();//父部门
					Map<String, String> selfPermission = perm.findOperatorResources(depart.getId());//自己的权限
					if(null == parentDepart){//无父，为顶级部门，将全部变电站中其他没进行设置过的变电站设置为拒绝。
						HashMap<String, Substation> substationMap = substationCacheMap;//全部站
						if(permissionMap.size() != substationMap.size()){
							for(Map.Entry<String, Substation> entry : substationMap.entrySet()){
								String key = entry.getKey();
								if(StringUtils.isEmpty(permissionMap.get(key)) && StringUtils.isEmpty(selfPermission.get(key))){//permissionMap中没有，且自己权限中没设置过
									permissionMap.put(key, PermissionTypeEnum.DENY.toString());
								}
							}
						}
					}else{//有父，将父级部门中允许的变电站中其他没进行设置过的变电站设置为拒绝。
						Map<String, String> parentPermission = perm.findOperatorResources(parentDepart.getId());//父部门权限
						for(Map.Entry<String, String> entry : parentPermission.entrySet()){
							String key = entry.getKey();
							if(StringUtils.isEmpty(permissionMap.get(key)) && StringUtils.isEmpty(selfPermission.get(key))){//permissionMap中没有，且自己权限中没设置过
								permissionMap.put(key, PermissionTypeEnum.DENY.toString());
							}
						}
					}
					perm.grantDeptPermission(depart.getId(), permissionMap);
					genOrgSubstationTreeByDepart(depart);//根据所选部门生成变电站权限树
					if(StringUtils.isNoneBlank(depart.getType())) {
						try {
							ManagementUnitEnum mue = ManagementUnitEnum.valueOf(depart.getType());
							if(mue != null)
								updateMsg = "您确定要将所选变电站的" +mue.getLabel()+ "设置为" + depart.getName() + "吗？";
						} catch (Exception e) {
						}
					}
					if(StringUtils.isNotEmpty(updateMsg)){
						PrimeFaces.current().ajax().update("resetStationLedgers");
						PrimeFaces.current().executeScript("resetStationLedgers()");//调用前台事件
					}
				}
			}else{//人员
				PersonnelEntityBean person = (PersonnelEntityBean) selectedNode.getData();
				if(null != substationSavedMap.get(person.getId())){
					perm.grantPersonPermission(person.getCode(), substationSavedMap.get(person.getId()));
				}
			}
		}
	}
	
	/**
	 * 生成dto
	 * @param id
	 * @param operator
	 * @param name
	 * @param resource
	 * @param permission
	 * @param standVoltagelevel
	 * @return dto
	 */
	public OrgSubstationDTO genOrgSubstationDTO(String id,String operator, String name, String resource, boolean permission, 
		String standVoltagelevel){
		OrgSubstationDTO org = new OrgSubstationDTO();
		org.setId(id);
		org.setOperator(operator);
		org.setName(name);
		org.setResource(resource);
		org.setPermission(permission);
		org.setStandVoltagelevel(standVoltagelevel);
		return org;
	}
	
	/**
	 * 一键重置组织结构缓存数据
	 */
	public void oneKeySet(){
		permissionCacheManager.reloadPersonPermission();
		addSuccessMessage("重置成功！");
	}
	
	/**
	 * 根据所选部门，将给定的变电站中对应类型的单位更新
	 * @param stationIds 变电站id
	 * @param dept 部门
	 */
	public void resetStationLedgers(){
		if(null != selectedNode){
			if(selectedNode.getData() instanceof DepartmentEntityBean){//部门
				DepartmentEntityBean depart = (DepartmentEntityBean) selectedNode.getData();
				if(null != substationSavedMap.get(depart.getId()) && StringUtils.isNotBlank(depart.getType())){
					ManagementUnitEnum mue = ManagementUnitEnum.valueOf(depart.getType());
					if(mue != null) {
						ManagementUnitDict mu = new ManagementUnitDict(mue, null, depart.getId(),
								depart.getName(), depart.getSno());
						Map<String, String> permissionMap = substationSavedMap.get(depart.getId());
						List<String> stationIds = new ArrayList<String>();//允许的站
						for(Map.Entry<String, String> entry : permissionMap.entrySet()){
							if(PermissionTypeEnum.ALLOW.toString().equals(entry.getValue())){//只添加允许的站
								stationIds.add(entry.getKey());
							}
						}
						substationLedgerManager.resetStationLedgers(stationIds, mu);
					}
				}
			}
		}
	}
	
	//以下是组织机构相关
	/**
	 * 初始化部门树
	 */
	public void initDeptTree() {
		deptRoot = new LibTreeNode("Root", null);
		List<DepartmentEntityBean> roots = dm.findRootDepts();
		for (DepartmentEntityBean s : roots) {
			TreeNode dRoot = new LibTreeNode("dept", s, deptRoot);
			List<DepartmentEntityBean> ds = dm.getDepartmentsByParent(s.getId()); // 默认展开一级
			for (DepartmentEntityBean d : ds) {
				new LibTreeNode("dept", d, dRoot);
			}
			dRoot.setExpanded(true);
		}
	}
	
	/**
	 * 加载员工信息列表
	 * @param selectedNode
	 */
	public void loadPersonnelList(TreeNode selectedNode){
		personnels.clear();
		if ("dept".equals(selectedNode.getType())) {
			currentDept = (DepartmentEntityBean) selectedNode.getData();
			personnels.addAll(dm.findDeptPersonnels(currentDept.getId()));
			currentPerson = null;
		} else if ("personnel".equals(selectedNode.getType())) {
			currentPerson = (PersonnelEntityBean) selectedNode.getData();
			PersonnelEntityBean personnel = pm.findPersonnelById(currentPerson.getId());
			personnels.add(personnel);
			currentDept = null;
		}
	}
	
	/**
	 * 新建部门前校验
	 */
	public void preCreateDept() {
		deptType = "";
		checkStatus= false;
		currentDept = new DepartmentEntityBean();
		if (selectedNode.getData() instanceof DepartmentEntityBean) {
			DepartmentEntityBean parentDepart = (DepartmentEntityBean) selectedNode.getData();
			List<DepartmentEntityBean> childDepts = dm.getDepartmentsByParent(parentDepart.getId());
			currentDept.setSno(childDepts.size());
			currentDept.setParent(parentDepart);
		}
	}

	/**
	 * 编辑部门前校验
	 */
	public void preEditDept() {
		checkStatus =false;//编辑时保存按钮不可用
		deptType = getDeptTypeName(currentDept.getType());
		if(selectedNode.getData() instanceof DepartmentEntityBean){//重新读取部门信息
			currentDept = dm.findDepartmentById(((DepartmentEntityBean) selectedNode.getData()).getId());
		}
	}
	
	/**
	 * 新建员工前校验
	 */
	public void preCreatePerson() {
		checkStatus=false;
		currentPerson = new PersonnelEntityBean();
		currentPerson.setTheme("omega");
		currentPerson.setPassword(MD5.md5(initPwd));
		if (selectedNode.getData() instanceof DepartmentEntityBean) {
			DepartmentEntityBean parentDepart = (DepartmentEntityBean) selectedNode.getData();
			List<PersonnelEntityBean> perList = dm.findDeptPersonnels(parentDepart.getId());
			currentPerson.setOrderNo(perList.size());
			currentPerson.setGroup(parentDepart);
		}
		selectedRoles = new String[]{};
	}
	
	/**
	 * 更新部门树
	 */
	public void updateDeptTree() {
		checkStatus =false;//编辑时保存按钮不可用
		findUserRoles();
		initDeptTree();
		preEditPerson();
	}
	
	public void findUserRoles(){
		Set<RoleEntityBean> roles = pm.findPersonRolesByCode(currentPerson.getCode());
		selectedRoles = new String[roles.size()];
		int i = 0;
		for (RoleEntityBean r : roles) {
			selectedRoles[i] = r.getId();
			i++;
		}
	}
	
	/**
	 * 编辑人员，用parentNode记录人员的之前的部门
	 */
	private void preEditPerson() {
		parentNode = selectedNode.getParent();
		if(selectedNode.getData() instanceof PersonnelEntityBean){//重新读取人员信息
			currentPerson = pm.findPersonnelById(((PersonnelEntityBean)selectedNode.getData()).getId());
		}
	}
	
	/**
	 * 验证部门名称不为空
	 * @param dept
	 */
	public void valdateDept(DepartmentEntityBean dept){
		if (StringUtils.isEmpty(dept.getName())) {
			FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "警告!", "部门名称不为空！"));
			checkStatus= false;
		}else{
			checkStatus= true;
		}
	}
	/**
	 * 验证用户姓名不为空，用户名唯一
	 * @param person
	 */
	public void valdatePerson(PersonnelEntityBean person){
		if(StringUtils.isEmpty(person.getCode())){
			FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "警告!", "用户名不允许为空！"));
			checkStatus= false;
		}else if(pm.checkCodeExist(person.getCode())){
			FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR, "警告!","用户名已存在！"));
			checkStatus= false;
		}else if(StringUtils.isEmpty(person.getName())){
			FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "警告!", "姓名不允许为空！"));
			checkStatus =false;
		}else{
			checkStatus= true;
		}
	}
	/**
	 * 验证用户姓名不为空
	 * @param person
	 */
	public void valdateEditPerson(PersonnelEntityBean person){
		if(StringUtils.isEmpty(person.getName())){
			FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "警告!", "姓名不允许为空！"));
			checkStatus =false;
		}else{
			checkStatus= true;
		}
	}

	/**
	 * 选择部门
	 */
	public void changeDept() {
		DepartmentEntityBean db = (DepartmentEntityBean) deptSelectedNode.getData();
		currentPerson.setGroup(db);
	}

	/**
	 * 部门树选择
	 * @param event
	 */
	public void onDeptNodeSelect(NodeSelectEvent event) {
		deptSelectedNode = event.getTreeNode();
	}

	/**
	 * 展开部门树(弹出框)
	 * @param event
	 */
	public void onDeptNodeExpand(NodeExpandEvent event) {
		TreeNode selectedNode = event.getTreeNode();
		DepartmentEntityBean selected = (DepartmentEntityBean) selectedNode.getData();
		List<DepartmentEntityBean> ds = dm.getDepartmentsByParent(((DepartmentEntityBean) selected).getId());
		for (DepartmentEntityBean dept : ds) {
			new LibTreeNode("dept", dept, selectedNode);
		}
	}
	
	/**
	 * 解锁人员
	 * @param pb
	 */
	public void unLockPerson(PersonnelEntityBean pb){
		pb.setLockedTime(null);
		pb.setLocked(false);
		try {
			pm.createOrUpdatePersonnel(pb);
		} catch (AuthorizationException e) {
			addErrorMessage(e.getMessage());
			e.printStackTrace();
		}
	}
	
	/**
	 * 重置密码前准备
	 */
	public void preResetPassword(String code){
		this.code = code;
		oldPwd=null;
		newPwd=null;
		newPwd1=null;
	}
	
	/**
	 * 重置密码
	 */
	public void updatePwd() { 
		currentPerson = pm.findPersonnelByCode(code);
		int result = pm.validatePassword(currentPerson, newPwd, newPwd1);
		if (result == 0) {
			pm.changePassword((currentPerson).getCode(), newPwd);
			addSuccessMessage("密码修改成功！");
		}  else if (result == 1) {
			addErrorMessage("两次新密码输入不一致!");
		}  else if (result == 2) {
			addErrorMessage("新密码不能与用户名相同!");
		} else if (result == 3) {
			addErrorMessage("新密码长度不能小于8!");
		} else if (result == 4) {
			addErrorMessage("新密码至少为字符、数字和符号其中两种组合!");
		}
	}
	
	/**
	 * 获取全部单位类型
	 * @return
	 */
	public List<String> getAllDeptType() {
		List<String> deptTypes = new ArrayList<String>();
		for (ManagementUnitEnum e : ManagementUnitEnum.values()) {
			deptTypes.add(e.getLabel());
		}
		return deptTypes;
	}
	
	/**
	 * 根据名称获取单位类型
	 * @return
	 */
	public String getDeptType(String name) {
		String type = null;
		for (ManagementUnitEnum e : ManagementUnitEnum.values()) {
			if(e.getLabel().equals(name)) {
				type = e.name();
			}
		}
		return type;
	}
	
	/**
	 * 根据类型获取单位名称
	 * @param type
	 * @return
	 */
	public String getDeptTypeName(String type) {
		String name = null;
		for (ManagementUnitEnum e : ManagementUnitEnum.values()) {
			if(e.name().equals(type)) {
				name = e.getLabel();
			}
		}
		return name;
	}

	/**
	 * 保存部门
	 */
	public void saveDept() {
		String type = getDeptType(deptType);
		currentDept.setType(type);
		if(StringUtils.isEmpty(currentDept.getId())){//新增
			currentDept.setId(ObjectId.get().toString());
			TreeNode treeNode = new LibTreeNode("dept", currentDept, selectedNode);//新增节点
			departmentMap.put(currentDept.getId(), treeNode);//存储节点
		}else{
			DepartmentEntityBean bean =(DepartmentEntityBean) selectedNode.getData();
			try {
				BeanUtils.copyProperties(bean, currentDept);//修改节点数据
			} catch (IllegalAccessException | InvocationTargetException e) {
				e.printStackTrace();
			}
			changeTreeNodeOrder(selectedNode);//修改所传入节点的部门、人员顺序
		}
		dm.createOrUpdateDepartment(currentDept);
		if(StringUtils.isNotEmpty(currentDept.getId())){//编辑后按钮不可用
			currentDept = null;//重置当前选择的部门
			currentPerson = null;
			selectedNode.setSelected(false);
			selectedNode = null;
		}
		addSuccessMessage("部门信息保存成功！");
	}
	
	/**
	 * 修改所传入节点的部门、人员顺序
	 * @param treeNode
	 */
	public void changeTreeNodeOrder(TreeNode treeNode){
		TreeNode parentNode = treeNode.getParent();
		parentNode.setSelected(false);
//		DepartmentEntityBean depart = (DepartmentEntityBean) treeNode.getData();
		Map<Integer, List<TreeNode>> departMap = new TreeMap<Integer, List<TreeNode>>();//序号-部门节点集合
		Map<Integer, List<TreeNode>> personMap = new TreeMap<Integer, List<TreeNode>>();//序号-人员节点集合
		if(null != parentNode && parentNode.getChildCount() > 0){
			for(TreeNode node : parentNode.getChildren()){
				node.setSelected(false);//解决自动选择上一次编辑的部门节点问题
				if (node.getType().equals("dept")) {//部门节点
					DepartmentEntityBean bean = (DepartmentEntityBean) node.getData();
					List<TreeNode> nodeList = departMap.get(bean.getSno());
					if(null == nodeList){
						nodeList = new ArrayList<TreeNode>();
					}
					nodeList.add(node);
					departMap.put(bean.getSno(), nodeList);
				}else{
					PersonnelEntityBean person = (PersonnelEntityBean) node.getData();
					List<TreeNode> nodeList = personMap.get(person.getOrderNo());
					if(null == nodeList){
						nodeList = new ArrayList<TreeNode>();
					}
					nodeList.add(node);
					personMap.put(person.getOrderNo(), nodeList);
				}
			}
			if(departMap.size() > 0){
				parentNode.getChildren().clear();
				for(Map.Entry<Integer, List<TreeNode>> entry : departMap.entrySet()){
					List<TreeNode> nodeList = entry.getValue();
					for(TreeNode tree : nodeList){
						DepartmentEntityBean bean = (DepartmentEntityBean) tree.getData();
						TreeNode tn = new LibTreeNode("dept", bean, parentNode);//新增节点
						departmentMap.put(bean.getId(), tn);//存储节点
//						if(depart.getId().equals(bean.getId())){//重新赋值当前选择的节点
//							selectedNode = tn;
//							tn.setSelected(true);
//						}
					}
				}
				for(Map.Entry<Integer, List<TreeNode>> entry : personMap.entrySet()){
					List<TreeNode> nodeList = entry.getValue();
					for(TreeNode tree : nodeList){
						PersonnelEntityBean person = (PersonnelEntityBean) tree.getData();
						new DefaultTreeNode("personnel", person, parentNode);//新增节点
					}
				}
			}
		}
	}
	
	/**
	 * 保存员工
	 */
	public void savePerson() {
		Set<RoleEntityBean> selRoles = new HashSet<RoleEntityBean>();
		for (int i = 0; i < selectedRoles.length; i++) {
			for (RoleEntityBean r : roles) {
				if (r.getId().equals(selectedRoles[i])) {
					selRoles.add(r);
					break;
				}
			}
		}
		if(StringUtils.isEmpty(currentPerson.getId())){
			currentPerson.setId(ObjectId.get().toString());//新建
		}
		currentPerson.setRoles(selRoles);
		try {
			pm.createOrUpdatePersonnel(currentPerson);
		} catch (AuthorizationException e) {
			addErrorMessage(e.getMessage());
			e.printStackTrace();
		}
		addSuccessMessage("员工信息保存成功！");
		refreshDeptNode();
		personnels = dm.findDeptPersonnels(currentDept.getId());
		// reloadOrgTree();
		permissionCacheManager.reloadPersonPermission(currentPerson.getCode());
		if(selectedNode.getData() instanceof PersonnelEntityBean) {
			currentDept = null;//重置当前选择的部门
			selectedNode.setSelected(false);
			selectedNode = null;
		}
		currentPerson = null;
	}
	
	/**
	 * 新建、编辑人员后，刷新当前节点，而不是整棵树
	 */
	private void refreshDeptNode() {
		if(selectedNode.getData() instanceof DepartmentEntityBean) {
			// 新建人员，只需先将原所属部门清空，再重新读入
			currentDept = (DepartmentEntityBean) selectedNode.getData();
			parentNode = selectedNode;
			new DefaultTreeNode("personnel", currentPerson, parentNode);//新增节点
		} else if (selectedNode.getData() instanceof PersonnelEntityBean) {
			if(null != deptSelectedNode) {
				// 编辑人员，原部门去除该人员
				parentNode.getChildren().remove(selectedNode);
				// 新部门先清空，再重新读入
				currentDept = (DepartmentEntityBean) deptSelectedNode.getData();
				parentNode = departmentMap.get(currentDept.getId());
				try {
					BeanUtils.copyProperties((PersonnelEntityBean)selectedNode.getData(), currentPerson);//移动部门的同时修改了数据
				} catch (IllegalAccessException | InvocationTargetException e) {
					e.printStackTrace();
				}
				if(null != parentNode){//若组织机构树节点没展开过，则子部门map中无对应数据，所以不自动处理节点。移完部门，手动展开后，会自动重读数据库。
					parentNode.getChildren().add(selectedNode);//追加节点
				}
			} else {
				currentDept = (DepartmentEntityBean) selectedNode.getParent().getData();
				parentNode = selectedNode.getParent();
				for(TreeNode treeNode : parentNode.getChildren()){//修改原节点
					if(treeNode.getData() instanceof PersonnelEntityBean){//员工节点
						PersonnelEntityBean person = (PersonnelEntityBean) treeNode.getData();
						if(person.getId().equals(currentPerson.getId())){
							try {
								BeanUtils.copyProperties(person, currentPerson);
								break;
							} catch (IllegalAccessException | InvocationTargetException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
			changePersonNodeOrder(selectedNode);//修改所传入人员节点的顺序
		}
		deptSelectedNode = null;
	}
	
	/**
	 * 修改所传入人员节点的顺序
	 * @param treeNode
	 */
	public void changePersonNodeOrder(TreeNode treeNode){
//		PersonnelEntityBean per = (PersonnelEntityBean) treeNode.getData();
		TreeNode parentNode = treeNode.getParent();
		if(null == parentNode)
			return;
		parentNode.setSelected(false);//解决自动选择上一次编辑的部门节点问题
		Map<Integer, List<TreeNode>> personMap = new TreeMap<Integer, List<TreeNode>>();//序号-人员节点集合
		if(null != parentNode && parentNode.getChildCount() > 0){
			Iterator<TreeNode> it = parentNode.getChildren().iterator();
			while(it.hasNext()){
				TreeNode node = it.next();
				node.setSelected(false);//解决自动选择上一次编辑的部门节点问题
				if (node.getType().equals("personnel")) {//人员节点
					PersonnelEntityBean person = (PersonnelEntityBean) node.getData();
					List<TreeNode> nodeList = personMap.get(person.getOrderNo());
					if(null == nodeList){
						nodeList = new ArrayList<TreeNode>();
					}
					nodeList.add(node);
					personMap.put(person.getOrderNo(), nodeList);
					it.remove();
				}
			}
			for(Map.Entry<Integer, List<TreeNode>> entry : personMap.entrySet()){
				List<TreeNode> nodeList = entry.getValue();
				for(TreeNode tree : nodeList){
					PersonnelEntityBean person = (PersonnelEntityBean) tree.getData();
					new DefaultTreeNode("personnel", person, parentNode);//新增节点
//					tn.setSelected(false);
//					if(per.getId().equals(person.getId())){//重新赋值当前选择的节点
//						selectedNode = tn;
//						tn.setSelected(true);
//					}
				}
			}
		}
	}

	/**
	 * 删除部门、人员
	 */
	public void delete() {
		if ("dept".equals(selectedNode.getType())) {
			DepartmentEntityBean d = (DepartmentEntityBean) selectedNode.getData();
			if (dm.findSubDepartments(d.getId()).size() > 0 || dm.findDeptPersonnels(d.getId()).size() > 0) {
				addErrorMessage("删除失败,该部门非空，不建议直接删除！");
				return;
			} else {
				dm.deleteDepartment(d.getId());
				selectedNode.getParent().getChildren().remove(selectedNode);
			}
		} else if ("personnel".equals(selectedNode.getType())) {
			PersonnelEntityBean p = (PersonnelEntityBean) selectedNode.getData();
			pm.deletePersonnelByCode(p.getCode());
			selectedNode.getParent().getChildren().remove(selectedNode);
			personnels.remove(p);
		}
		currentDept = null;//重置当前选择的部门
		currentPerson = null;
		selectedNode = null;
		addSuccessMessage("删除成功");
	}
	
	public class OrgFunctionDTO {
		private FunctionEntityBean function;
		private String permission; 

		public OrgFunctionDTO() {
		}

		public OrgFunctionDTO(FunctionEntityBean bean, String permiss) {
			this.function = bean;
			this.permission = permiss;
		}

		public FunctionEntityBean getFunction() {
			return function;
		}

		public void setFunction(FunctionEntityBean function) {
			this.function = function;
		}


		public String getPermission() {
			return permission;
		}

		public void setPermission(String permission) {
			this.permission = permission;
		}
	}

	public class RolePersonDTO{
		private String name;
		private String telphone;
		private String status;
		public RolePersonDTO() {
			super();
		}
		public RolePersonDTO(String name, String telphone, String status) {
			super();
			this.name = name;
			this.telphone = telphone;
			this.status = status;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getTelphone() {
			return telphone;
		}
		public void setTelphone(String telphone) {
			this.telphone = telphone;
		}
		public String getStatus() {
			return status;
		}
		public void setStatus(String status) {
			this.status = status;
		}
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + getOuterType().hashCode();
			result = prime * result + ((name == null) ? 0 : name.hashCode());
			result = prime * result
					+ ((status == null) ? 0 : status.hashCode());
			result = prime * result
					+ ((telphone == null) ? 0 : telphone.hashCode());
			return result;
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			RolePersonDTO other = (RolePersonDTO) obj;
			if (!getOuterType().equals(other.getOuterType()))
				return false;
			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equals(other.name))
				return false;
			if (status == null) {
				if (other.status != null)
					return false;
			} else if (!status.equals(other.status))
				return false;
			if (telphone == null) {
				if (other.telphone != null)
					return false;
			} else if (!telphone.equals(other.telphone))
				return false;
			return true;
		}
		private OrganizationPermissionController4Nw getOuterType() {
			return OrganizationPermissionController4Nw.this;
		}
	}
	
	/**
	 * 部门/人员的变电站权限内部类
	 * @date 2018-12-28
	 *
	 */
	public class OrgSubstationDTO{
		private String id;
		private String operator;//操作者id（如部门id、人员id）
		private String name;//操作者名称（如部门名称、人员名称）
		private String resource;//功能
		private boolean permission;//权限
		private String standVoltagelevel;//电压等级
		public OrgSubstationDTO() {
			super();
		}
		public OrgSubstationDTO(String id, String operator, String name,
				String resource, boolean permission, String standVoltagelevel) {
			super();
			this.id = id;
			this.operator = operator;
			this.name = name;
			this.resource = resource;
			this.permission = permission;
			this.standVoltagelevel = standVoltagelevel;
		}
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getOperator() {
			return operator;
		}
		public void setOperator(String operator) {
			this.operator = operator;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getResource() {
			return resource;
		}
		public void setResource(String resource) {
			this.resource = resource;
		}
		public boolean getPermission() {
			return permission;
		}
		public void setPermission(boolean permission) {
			this.permission = permission;
		}
		public String getStandVoltagelevel() {
			return standVoltagelevel;
		}
		public void setStandVoltagelevel(String standVoltagelevel) {
			this.standVoltagelevel = standVoltagelevel;
		}
	}
	
	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getFuncRoot() {
		return funcRoot;
	}

	public void setFuncRoot(TreeNode funcRoot) {
		this.funcRoot = funcRoot;
	}

	public TreeNode getSelectedNode() {
		return selectedNode;
	}

	public void setSelectedNode(TreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}

	public TreeNode getGrantRoot() {
		return grantRoot;
	}

	public void setGrantRoot(TreeNode grantRoot) {
		this.grantRoot = grantRoot;
	}

	public TreeNode getRoleRoot() {
		return roleRoot;
	}

	public void setRoleRoot(TreeNode roleRoot) {
		this.roleRoot = roleRoot;
	}

	public TreeNode getRoleSelectedNode() {
		return roleSelectedNode;
	}

	public void setRoleSelectedNode(TreeNode roleSelectedNode) {
		this.roleSelectedNode = roleSelectedNode;
	}

	public RoleEntityBean getCurrentSelectRole() {
		return currentSelectRole;
	}

	public void setCurrentSelectRole(RoleEntityBean currentSelectRole) {
		this.currentSelectRole = currentSelectRole;
	}

	public TreeNode getRolePersonRoot() {
		return rolePersonRoot;
	}

	public void setRolePersonRoot(TreeNode rolePersonRoot) {
		this.rolePersonRoot = rolePersonRoot;
	}

	public TreeNode[] getSelectedNodes() {
		return selectedNodes;
	}

	public void setSelectedNodes(TreeNode[] selectedNodes) {
		this.selectedNodes = selectedNodes;
	}

	public TreeNode[] getRoleSelectedNodes() {
		return roleSelectedNodes;
	}

	public void setRoleSelectedNodes(TreeNode[] roleSelectedNodes) {
		this.roleSelectedNodes = roleSelectedNodes;
	}

	public List<RoleEntityBean> getPersonRoles() {
		return personRoles;
	}

	public void setPersonRoles(List<RoleEntityBean> personRoles) {
		this.personRoles = personRoles;
	}

	public String getGrantPerTitle() {
		return grantPerTitle;
	}

	public void setGrantPerTitle(String grantPerTitle) {
		this.grantPerTitle = grantPerTitle;
	}


	public String getStationSearch() {
		return stationSearch;
	}

	public void setStationSearch(String stationSearch) {
		this.stationSearch = stationSearch;
	}

	public TreeNode getSubstationRoot() {
		return substationRoot;
	}

	public void setSubstationRoot(TreeNode substationRoot) {
		this.substationRoot = substationRoot;
	}

	public TreeNode getOrgSubstationRoot() {
		return orgSubstationRoot;
	}

	public void setOrgSubstationRoot(TreeNode orgSubstationRoot) {
		this.orgSubstationRoot = orgSubstationRoot;
	}

	public String getSubstationRadioValue() {
		return substationRadioValue;
	}

	public void setSubstationRadioValue(String substationRadioValue) {
		this.substationRadioValue = substationRadioValue;
	}

	public String getUpdateMsg() {
		return updateMsg;
	}

	public void setUpdateMsg(String updateMsg) {
		this.updateMsg = updateMsg;
	}

	public boolean isValue() {
		return value;
	}

	public void setValue(boolean value) {
		this.value = value;
	}

	public TreeNode getDeptRoot() {
		return deptRoot;
	}

	public void setDeptRoot(TreeNode deptRoot) {
		this.deptRoot = deptRoot;
	}

	public DepartmentEntityBean getCurrentDept() {
		return currentDept;
	}

	public void setCurrentDept(DepartmentEntityBean currentDept) {
		this.currentDept = currentDept;
	}

	public PersonnelEntityBean getCurrentPerson() {
		return currentPerson;
	}

	public void setCurrentPerson(PersonnelEntityBean currentPerson) {
		this.currentPerson = currentPerson;
	}

	public List<PersonnelEntityBean> getPersonnels() {
		return personnels;
	}

	public void setPersonnels(List<PersonnelEntityBean> personnels) {
		this.personnels = personnels;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public String getNewPwd1() {
		return newPwd1;
	}

	public void setNewPwd1(String newPwd1) {
		this.newPwd1 = newPwd1;
	}

	public boolean isCheckStatus() {
		return checkStatus;
	}

	public void setCheckStatus(boolean checkStatus) {
		this.checkStatus = checkStatus;
	}

	public String getRolesName() {
		return rolesName;
	}

	public void setRolesName(String rolesName) {
		this.rolesName = rolesName;
	}

	public List<RoleEntityBean> getRoles() {
		return roles;
	}

	public void setRoles(List<RoleEntityBean> roles) {
		this.roles = roles;
	}

	public String[] getSelectedRoles() {
		return selectedRoles;
	}

	public void setSelectedRoles(String[] selectedRoles) {
		this.selectedRoles = selectedRoles;
	}

	public String getDeptType() {
		return deptType;
	}

	public void setDeptType(String deptType) {
		this.deptType = deptType;
	}
	public HashMap<String, Substation> getSubstationCacheMap() {
		return substationCacheMap;
	}
	public void setSubstationCacheMap(HashMap<String, Substation> substationCacheMap) {
		this.substationCacheMap = substationCacheMap;
	}
	public String getInitPwd() {
		return initPwd;
	}
	public void setInitPwd(String initPwd) {
		this.initPwd = initPwd;
	}
}
